<?php

namespace App\Http\Controllers\Api\V1;

use App\Models\Favor;
use Illuminate\Http\Request;

class FavorController extends BaseController
{
    /**
     * 判断用户是否收藏
     * @param  [type] $token [description]
     * @return [type]        [description]
     */
    public function isFavor($token, $assetType, $assetId, Request $request)
    {

        // 是否被收藏 1 已收藏 0 未收藏
        if (Favor::where(['asset_type' => $assetType, 'asset_id' => $assetId, 'user_id' => $userId])->first()) {
            $data['favorite_status'] = 1;
            $data['info']            = '已收藏';
        } else {
            $data['favorite_status'] = 0;
            $data['info']            = '未收藏';
        }
        return response()->json(['status' => '200', 'data' => $data]);
    }

    /**
     * post方式添加收藏
     * @param  Request $request [description]
     * @return [type]           [description]
     */
    public function postStore(Request $request)
    {
        // 数据类型
        $assetType = $request->input('asset_type');
        if (empty($assetType)) {
            return response()->json(['status' => '404', 'data' => '媒资类型为空']);
        }
        // 资源ID
        $assetId = $request->input('asset_id');
        if (empty($assetId)) {
            return response()->json(['status' => '404', 'data' => '媒资ID为空']);
        }
        // token
        $token = $request->input('token');
        if (empty($token)) {
            return response()->json(['status' => '404', 'data' => 'token为空']);
        }
        $userId             = $this->getUserIdByToken($token);
        $data               = [];
        $data['asset_type'] = $assetType;
        $data['asset_id']   = $assetId;
        $data['user_id']    = $userId;
        if (Favor::where(['user_id' => $userId, 'asset_type' => $assetType, 'asset_id' => $assetId])->first()) {
            return response()->json(['status' => '404', 'data' => '已收藏!']);
        }
        // 创建收藏
        $result = Favor::create($data);
        if (!$result) {
            return response()->json(['status' => '404', 'data' => '收藏失败!']);
        } else {
            return response()->json(['status' => '200', 'data' => '收藏成功!']);
        }
    }

    /**
     * 取消收藏
     * @param  [type]  $token     [description]
     * @param  [type]  $assetType [description]
     * @param  [type]  $assetId   [description]
     * @param  Request $request   [description]
     * @return [type]             [description]
     */
    public function delete(Request $request, $token, $assetType, $assetId)
    {
        // 根据token获取用户ID
        $userId = $this->getUserIdByToken($token);
        if (!Favor::where(['user_id' => $userId, 'asset_type' => $assetType, 'asset_id' => $assetId])->first()) {
            return response()->json(['status' => '404', 'data' => '无此收藏!']);
        }

        if (Favor::where(['user_id' => $userId, 'asset_type' => $assetType, 'asset_id' => $assetId])->delete()) {
            return response()->json(['status' => '200', 'data' => '取消收藏成功!']);
        } else {
            return response()->json(['status' => '404', 'data' => '取消收藏失败!']);
        }
    }
    /**
     * 删除用户的全部收藏
     * @param  Request $request [description]
     * @param  [type]  $token   [description]
     * @return [type]           [description]
     */
    public function deleteAllFavor(Request $request, $token)
    {
        // 根据token获取用户ID
        $userId = $this->getUserIdByToken($token);
        $result = Favor::where(['user_id' => $userId])->delete();
        if ($result) {
            return response()->json(['status' => '200', 'data' => '取消收藏成功!']);
        } else {
            return response()->json(['status' => '404', 'data' => '取消收藏失败!']);
        }
    }
    /**
     * 删除用户的全部收藏
     * @param  Request $request [description]
     * @param  [type]  $token   [description]
     * @return [type]           [description]
     */
    public function deleteAllFavorByType(Request $request, $token, $assetType)
    {
        // 根据token获取用户ID
        $userId = $this->getUserIdByToken($token);
        $result = Favor::where(['user_id' => $userId, 'asset_type' => $assetType])->delete();
        if ($result) {
            return response()->json(['status' => '200', 'data' => '取消收藏成功!']);
        } else {
            return response()->json(['status' => '404', 'data' => '取消收藏失败!']);
        }
    }

    /**
     * 获取当前收藏列表
     * @param  [type]  $token   [description]
     * @param  Request $request [description]
     * @return [type]           [description]
     */
    public function getFavorsByUserId(Request $request, $token)
    {
        $token = 1;
        // 根据token获取用户ID
        $userId = $this->getUserIdByToken($token);
        $limit  = $request->input('limit') ? $request->input('limit') : 10;

        $favors = Favor::where(['user_id' => $userId])->paginate($limit);
        $data   = [];
        $page   = [];

        if (!empty($favors->first())) {
            foreach ($favors as $key => $value) {
                $data[$key]['id']         = $value->id;
                $data[$key]['user_id']    = $value->user_id;
                $data[$key]['asset_type'] = $value->asset_type;
                $data[$key]['asset_id']   = $value->asset_id;
            }
            $favorsPages              = $favors->toArray();
            $page['nowpage']          = $favorsPages['current_page'];
            $page['totalpage']        = $favorsPages['last_page'];
            $page['prepage']['href']  = $favorsPages['prev_page_url'];
            $page['nextpage']['href'] = $favorsPages['next_page_url'];
        }

        return response()->json(['status' => '200', 'data' => $data, 'page' => $page]);
    }

    /**
     * 根据类型获取收藏列表
     * @param  [type]  $assetType [description]
     * @param  Request $request   [description]
     * @return [type]             [description]
     */
    public function getFavorByAssetType(Request $request, $userId, $assetType)
    {
        // 根据token获取用户ID
        $userId = $this->getUserIdByToken($userId);
        $limit  = $request->input('limit') ? $request->input('limit') : 10;
        $favors = Favor::where(['user_id' => $userId, 'asset_type' => $assetType])->paginate($limit);
        $datas  = [];
        $data   = [];
        $page   = [];
        if (!empty($favors->first())) {
            foreach ($favors as $key => $value) {
                $data[$key]['id']         = $value->id;
                $data[$key]['user_id']    = $value->user_id;
                $data[$key]['asset_type'] = $value->asset_type;
                $data[$key]['asset_id']   = $value->asset_id;
            }
            $favorsPages              = $favors->toArray();
            $page['nowpage']          = $favorsPages['current_page'];
            $page['totalpage']        = $favorsPages['last_page'];
            $page['prepage']['href']  = $favorsPages['prev_page_url'];
            $page['nextpage']['href'] = $favorsPages['next_page_url'];
        }

        return response()->json(['status' => '200', 'data' => $data, 'page' => $page]);
    }

    /**
     * 根据token获取用户ID
     * @param  [type] $token [description]
     * @return [type]        [description]
     */
    public function getUserIdByToken($token)
    {
        // 检查token是否有效
        $userId = 1;
        return $userId;
    }

    /**
     * 用户收藏媒资
     * @param  [type]  $token     [description]
     * @param  [type]  $assetType [description]
     * @param  [type]  $assetId   [description]
     * @param  Request $request   [description]
     * @return [type]             [description]
     */
    // public function getStore($token, $assetType, $assetId, Request $request)
    // {

    //     // 根据机顶盒ID查找相关收藏表信息
    //     if (Favor::where(['asset_type' => $assetType, 'asset_id' => $assetId, 'user_id' => $userId])->first()) {
    //         return response()->json(['status' => '404', 'data' => '该资源已收藏!']);
    //     }
    //     $data               = [];
    //     $data['asset_type'] = $assetType;
    //     $data['asset_id']   = $assetId;
    //     $data['user_id']    = $token;

    //     $result = Favor::create($data);
    //     if (!$result) {
    //         return response()->json(['status' => '404', 'data' => '收藏失败!']);
    //     } else {
    //         return response()->json(['status' => '200', 'data' => '收藏成功!']);
    //     }
    // }

}
